<template>
  <div class="json-editor">
    <textarea ref="jsonEditor" class="editor-container-textarea-style" style="display: none" />
    <el-select class="code-mode-select" v-model="options.mode" @change="changeMode">
      <el-option v-for="mode in modes" :key="mode.value" :label="mode.label" :value="mode.value">
      </el-option>
    </el-select>
  </div>
</template>

<script>
/* eslint-disable no-param-reassign */
import _CodeMirror from 'hussar-codemirror';
import { JSHINT } from 'jshint';

import 'hussar-codemirror/lib/codemirror.css';
import 'hussar-codemirror/theme/idea.css';
import 'hussar-codemirror/theme/cobalt.css'

import 'hussar-codemirror/mode/clike/clike.js'
import 'hussar-codemirror/mode/r/r.js'
import 'hussar-codemirror/mode/shell/shell.js'
import 'hussar-codemirror/mode/swift/swift.js'
import 'hussar-codemirror/mode/vue/vue.js'
// import 'hussar-codemirror/keymap/sublime'
import "hussar-codemirror/mode/javascript/javascript.js";
import "hussar-codemirror/mode/xml/xml.js";
// import "hussar-codemirror/mode/htmlmixed/htmlmixed.js";
import "hussar-codemirror/mode/css/css.js";
import "hussar-codemirror/mode/yaml/yaml.js";
import "hussar-codemirror/mode/sql/sql.js";
import "hussar-codemirror/mode/python/python.js";
import "hussar-codemirror/mode/markdown/markdown.js";
//提示
// import "hussar-codemirror/addon/hint/show-hint.css";
// import "hussar-codemirror/addon/hint/show-hint.js";
// import "hussar-codemirror/addon/hint/javascript-hint.js";
// import "hussar-codemirror/addon/hint/xml-hint.js";
// import "hussar-codemirror/addon/hint/css-hint.js";
// import "hussar-codemirror/addon/hint/html-hint.js";
// import "hussar-codemirror/addon/hint/sql-hint.js";
// import "hussar-codemirror/addon/hint/anyword-hint.js";
//校验
import "hussar-codemirror/addon/lint/lint.css";
import "hussar-codemirror/addon/lint/lint.js";
import "hussar-codemirror/addon/lint/json-lint";
import 'hussar-codemirror/addon/display/autorefresh' //及时自动更新，配置里面也需要设置autoRefresh为true
import "hussar-codemirror/addon/lint/javascript-lint.js";
require("script-loader!jsonlint");

//折叠
import "hussar-codemirror/addon/fold/foldcode.js";
import "hussar-codemirror/addon/fold/foldgutter.js";
import "hussar-codemirror/addon/fold/foldgutter.css";
import "hussar-codemirror/addon/fold/brace-fold.js";
import "hussar-codemirror/addon/fold/xml-fold.js";
import "hussar-codemirror/addon/fold/comment-fold.js";
import "hussar-codemirror/addon/fold/markdown-fold.js";
import "hussar-codemirror/addon/fold/indent-fold.js";


// eslint-disable-next-line import/no-webpack-loader-syntax

window.JSHINT = JSHINT;
const CodeMirror = window.CodeMirror || _CodeMirror;
export default {
  name: 'JsonEditor',
  props: {
    oldValue: {
      type: Object
    },
  },
  data() {
    return {
      code: '<template><div>111</div></template>',
      newValue: {
      },   
      coder: null,
      // 默认的语法类型
      options: {
        mode: 'vue',
        autoRefresh: true,
        lineWrapping: true, // 横向超出换行
        scrollbarStyle: 'native', //滚动条
        autoCloseBrackets: true,
        matchBrackets: true, //匹配括号
        styleActiveLine: true, // 高亮选中行
        theme: 'idea', //主题
        lint: true,  // 检查格式
        // 代码折叠
        gutters: [
          "CodeMirror-lint-markers",
          "CodeMirror-linenumbers",
          "CodeMirror-foldgutter"
        ],
        foldGutter: true, // 启用行槽中的代码折叠
        lineNumbers: true, // 显示行号
        indentUnit: 0, //缩进单位
        hintOptions: {
          completeSingle: false
        }
      },
      modes: [{
        value: 'css',
        label: 'CSS'
      }, {
        value: 'javascript',
        label: 'Javascript'
      }, {
        value: 'html',
        label: 'XML/HTML'
      }, {
        value: 'x-java',
        label: 'Java'
      }, {
        value: 'x-objectivec',
        label: 'Objective-C'
      }, {
        value: 'x-python',
        label: 'Python'
      }, {
        value: 'x-rsrc',
        label: 'R'
      }, {
        value: 'x-sh',
        label: 'Shell'
      }, {
        value: 'x-sql',
        label: 'SQL'
      }, {
        value: 'x-swift',
        label: 'Swift'
      }, {
        value: 'x-vue',
        label: 'vue'
      }, {
        value: 'markdown',
        label: 'Markdown'
      }],
    };
  },
  watch: {},
  mounted() {
    this.initJsonEditor();
  },
  created() {

  },
  watch:{
        oldValue() {
          this.coder.setValue(this.oldValue.fileContent)
        }
    },
  methods: {
    /**
     * 初始化整个代码编辑器
     */
    initJsonEditor() {
      const self = this;
      self.$nextTick(() => {
        // 初始化编辑器实例，传入需要被实例化的文本域对象和默认配置
        this.coder = CodeMirror.fromTextArea(this.$refs.jsonEditor, this.options)
        // 编辑器赋值
        this.coder.setValue(this.code)
        // 支持双向绑定
        this.coder.on('change', (coder) => {
          this.code = coder.getValue()
          // this.coder.showHint();
          this.newValue.filePath = _.cloneDeep(this.oldValue.filePath);
          this.newValue.fileContent = _.cloneDeep(this.code);
          this.newValue.fileName = _.cloneDeep(this.oldValue.fileName)
          this.$emit('handleChangeJson', this.newValue);
        });
        console.log(this.code)
      });
    },
    // 更改模式
    changeMode(val) {
      // 修改编辑器的语法配置
      this.coder.setOption('mode', `text/${val}`)
    },
  }
};
</script>

<style lang="scss" scoped>
.json-editor {
  margin: 0 20px 25px 0;
  height: 92%;
  position: relative;

  ::v-deep .CodeMirror {
    border: 1px solid #e7e7e7;
    height: 100%;
    .CodeMirror-vscrollbar{
      left: auto;
      right: 0;
    }
     .CodeMirror-linenumber {
      width: 30px;
    }
  }

  &.is-disabled {
    cursor: not-allowed;
    ::v-deep .CodeMirror {
      background-color: #F4F4F4;
      cursor: not-allowed;
      pointer-events: none;
    }
  }
  .code-mode-select{
    position: absolute;
    top: -93px;
    width: 200px;
  }
}
</style>
<style>
.json-editor .el-select .el-input__inner{
  height: 32px;
  line-height: 32px;
}
</style>

